package com.spark.lola.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spark.lola.entity.sup.SupplierInfo;
import com.spark.lola.logistics.entity.CarSubscribe;
import com.spark.lola.logistics.mapper.CarSubscribeMapper;
import com.spark.lola.mapper.sup.SupplierInfoMapper;
import com.spark.lola.support.common.constant.CommonConstant;
import com.spark.lola.support.core.constant.SystemConstant;
import com.spark.lola.support.mybatis.entity.QueryParam;
import com.spark.lola.support.wms.configuration.WmsProperties;
import org.apache.poi.ss.usermodel.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author: DengYeJun
 * @Date: 2020/6/29 15:28
 */
@Component
public class SuppierJob {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Value("${car-subscribe.expire-time}")
    private Integer expireTime;

    @Autowired
    private SupplierInfoMapper supplierInfoMapper;

    @Autowired
    private CarSubscribeMapper carSubscribeMapper;

    @Autowired
    private WmsProperties wmsProperties;

    /**
     * 检测过期供应商
     */
    @Scheduled(cron = "0 0 0 * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void checkSupplier() {
        logger.info("===检测低活跃度供应商===START");
        Calendar endDate = Calendar.getInstance();
        endDate.add(Calendar.DATE, -wmsProperties.getService().getExpires().get("reset"));
        QueryWrapper<SupplierInfo> query = new QueryWrapper<>();
        query.le("update_time", endDate.getTime());
        List<SupplierInfo> lst = supplierInfoMapper.selectList(query);
        if (CollectionUtils.isEmpty(lst) || lst.size() == 0) {
            logger.warn("===无满足条件供应商，检测跳出...");
            return;
        }
        for (SupplierInfo o : lst) {
            logger.info("===重置供应商【{}】解绑业务员...", o.getName());
            supplierInfoMapper.resetSalesman(o.getId());
        }
        logger.info("===检测低活跃度供应商===END");
    }

    /**
     * 检查过期回头车
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void checkSubscribeCar() {
        logger.info("===检测3天未预约回头车===START");
        Calendar endDate = Calendar.getInstance();
        endDate.add(Calendar.DATE, -expireTime);
        QueryWrapper<CarSubscribe> query = new QueryWrapper<>();
        query.eq("used", CommonConstant.W);
        query.le("create_time", endDate.getTime());
        List<CarSubscribe> lst = carSubscribeMapper.selectList(query);
        if (CollectionUtils.isEmpty(lst)) {
            logger.warn("===无满足条件回头车，检测跳出...");
            return;
        }
        for (CarSubscribe o : lst) {
            logger.info("===设置回头车过期【{}】...", o.getCarNo());
            carSubscribeMapper.updateUsedById(CommonConstant.N, new Date(), o.getId());
        }
        logger.info("===检测3天未预约回头车===END");
    }

    public static void main(String[] main) {
        Calendar calc = Calendar.getInstance();
        calc.add(Calendar.DATE, -3);
        System.out.println(calc.getTime());
    }
}
